﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Threading;

namespace GUI
{
    public partial class usMachSac : UserControl
    {
        public usMachSac()
        {
            InitializeComponent();
        }

        private void TrangThai(bool status)
        {
            //Trạng Thái số giờ nắng
            tsThem.Enabled = status;
            tshuy.Enabled = !status;

        }

        private void usMachSac_Load(object sender, EventArgs e)
        {
            HienThiMachSac();
            SetupDataGridView(gvSanPham);
            TrangThai(true);
            
        }

        DataTable dtmdk = new DataTable();
       
        private void tsThem_Click(object sender, EventArgs e)
        {
            reset();
            strMaHienTai = "";
            strTenTB = "";
            grbDieuKhienSac.Visible = true;
            btnThem.Visible = true;
            btnsua.Visible = false;
            btnHuy.Enabled = true;
            TrangThai(false);
            txtma.Focus();
        }
        private bool kiemtrarong()
        {
            if (string.IsNullOrEmpty(txtten.Text) || string.IsNullOrEmpty(txtCS.Text))
            {
                MessageBox.Show("Nhập đầy đủ thông tin các trường có dấu (*)", "Thông Báo!!");
                return false;
            }
            else
                return true;
        }

        private void HienThiMachSac()
        {
            ThietBiDienBUS _mdk = new ThietBiDienBUS();
            dtmdk = _mdk.LoadMachDieuKhien();
            gvSanPham.DataSource = dtmdk;
        }

            private void SetupDataGridView(DataGridView gv)
        {
            DataGridViewCellStyle style = gv.ColumnHeadersDefaultCellStyle;
            style.BackColor = Color.Navy;
            style.ForeColor = Color.White;
            style.Font = new Font(gv.Font, FontStyle.Bold);


            gv.GridColor = Color.Blue;  //' Màu các của lưới
            gv.BackgroundColor = Color.AliceBlue; //' Màu nền gridview
            gv.ForeColor = Color.Green; //' Màu chữ
            //' Thiết lập màu chữ cho những dòng có chỉ số lẻ
            gv.AlternatingRowsDefaultCellStyle.ForeColor = Color.Purple;
        }

            private void btnThem_Click(object sender, EventArgs e)
            {
                try
                {
                    if (kiemtrarong())
                    {

                        ThongSoChungDTO _ts = new ThongSoChungDTO();
                        MachSacBus _tsBUS = new MachSacBus();
                        _ts.MATB = txtma.Text;
                        _ts.TENTB = txtten.Text;
                        //_ts.MATS = 1;
                        _ts.NGAYNHAP = Convert.ToDateTime(dpngaynhap.Text);
                        _ts.CSKHADUNG = Decimal.Parse(txtCS.Text);
                        if (string.IsNullOrEmpty(txtGia.Text))
                            _ts.DONGIA = 0;
                        else
                            _ts.DONGIA = int.Parse(txtGia.Text);

                        ThietBiDienDTO _tb = new ThietBiDienDTO();

                        _tb.VOL = 0;
                        _tb.AMP = 0;

                        _tb.LOAITB = 1;
                        _tb.GHICHU = txtghichu.Text;

                        _tsBUS.ThemMachSac(_ts, _tb);

                        reset();
                        HienThiMachSac();
                        txtma.Focus();
                        strMaHienTai = "";
                        strTenTB = "";
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Sai Cứu Pháp!!");
                }
            }

        private void btnsua_Click(object sender, EventArgs e)
        {
            try
            {
                sua();
                btnsua.Visible = false;
                //grbDieuKhienSac.Visible = false;
                btnThem.Visible = true;
                btnHuy.Enabled = true;
                strMaHienTai = "";
                strTenTB = "";
            }
            catch (Exception)
            {
                MessageBox.Show("Sai Cứu Pháp!!");
            }
            
        }

        private void sua()
        {
            ThongSoChungDTO _ts = new ThongSoChungDTO();
            MachSacBus _tsBUS = new MachSacBus();
            _ts.MATB = txtma.Text;
            _ts.TENTB = txtten.Text;
            _ts.NGAYNHAP = Convert.ToDateTime(dpngaynhap.Text);
            _ts.CSKHADUNG = Decimal.Parse(txtCS.Text);
            _ts.DONGIA = int.Parse(txtGia.Text);

            ThietBiDienDTO _tb = new ThietBiDienDTO();
            _tb.STT = iSTT;
            _tb.VOL = 0;
            _tb.AMP = 0;
            _tb.LOAITB = 0;
            _tb.GHICHU = txtghichu.Text;

            _tsBUS.SuaMachSac(_ts, _tb);

            reset();

            HienThiMachSac();

        }

        private void reset()
        {
            txtten.Text = "";
            dpngaynhap.Text = (DateTime.Now).ToString();
            //txtampe.Text = "";
            txtCS.Text = "";
            txtma.Text = "";
            //txtvol.Text = "";
            txtghichu.Text = "";
            txtGia.Text = "";
        }
        private void txtvol_TextChanged(object sender, EventArgs e)
        {
            //if (txtvol.Text != "" && txtampe.Text != "")
            //{
            //    txtCS.Text = (float.Parse(txtvol.Text) * float.Parse(txtampe.Text)).ToString();
            //}
        }

        private void txtampe_TextChanged(object sender, EventArgs e)
        {
            //if (txtvol.Text != "" && txtampe.Text != "")
            //{
            //    txtCS.Text = (float.Parse(txtvol.Text) * float.Parse(txtampe.Text)).ToString();
            //}
        }

        private void tshuy_Click(object sender, EventArgs e)
        {
            grbDieuKhienSac.Visible = false;
            TrangThai(true);
            strMaHienTai = "";
            strTenTB = "";
        }

        int ma_sua;
        int iSua = 0;
        string strMaHienTai;
        int iSTT;
        private void gvSanPham_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void gvSanPham_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            int rowIndex = e.RowIndex;
            if (rowIndex >= 0)
            {
                tshuy.Enabled = true;
                btnThem.Visible = false;
                btnsua.Visible = true;
                grbDieuKhienSac.Visible = true;
                iSua = 1;
                btnHuy.Enabled = false;
  

                DataGridViewRow row = gvSanPham.Rows[rowIndex];
                iSTT = int.Parse(row.Cells["STT"].Value.ToString());
                strMaHienTai = row.Cells["MaTB"].Value.ToString().Trim();
                strTenTB = row.Cells["TenTB"].Value.ToString();
                txtten.Text = row.Cells["TenTB"].Value.ToString();
                dpngaynhap.Text = row.Cells["NgayNhap"].Value.ToString();
               // txtampe.Text = row.Cells["Amp"].Value.ToString();
                txtCS.Text = row.Cells["CSKhaDung"].Value.ToString();
                txtma.Text = row.Cells["MaTB"].Value.ToString().Trim();
               // txtvol.Text = row.Cells["Vol"].Value.ToString();
                txtghichu.Text = row.Cells["GhiChu"].Value.ToString();
                txtGia.Text = row.Cells["DonGia"].Value.ToString();

            }
        }

        private void txtma_Leave(object sender, EventArgs e)
        {
            if (txtma.Text != "")
            {
                if (txtma.Text != strMaHienTai)
                {
                    ThongSoChungDTO _ts = new ThongSoChungDTO();
                    _ts.MATB = txtma.Text;
                    MachSacBus _tsBUS = new MachSacBus();
                    int dt = _tsBUS.KiemTra_MaTB(_ts);
                    if (dt == 1)
                    {
                        txtma.Text = strMaHienTai;
                        MessageBox.Show("Thiết Bị Đã Tồn Tại, Bạn Có Thể Cập Nhật Lại Thiết Bị", "Thông Báo!");
                        txtma.Focus();
                    }
                }

            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            reset();
            TrangThai(true);
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            reset();
            grbDieuKhienSac.Visible = false;
            TrangThai(true);
            strMaHienTai = "";
            strTenTB = "";
        }

        private void gvSanPham_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (MessageBox.Show("Bạn có chắc muốn xóa !", "Thông Báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {

                    MachSacBus _msBus = new MachSacBus();
                    int sTT = int.Parse(gvSanPham.CurrentRow.Cells["STT"].Value.ToString());

                    _msBus.XoaMachSac(sTT);



                    HienThiMachSac();


                }
            }
        }

        private void btnHuy_Click_1(object sender, EventArgs e)
        {
            reset();
            TrangThai(true);
        }

        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            //{
            //    e.Handled = true;
            //}
            //if (e.KeyChar != ',' && e.KeyChar != '.' && e.KeyChar != (char)46 && e.KeyChar != (char)8)
            //{
            //    if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
            //    {
            //        e.Handled = true;
                   
            //    }
            //}
        
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);
            TextBox a = sender as TextBox;
            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && a.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }
   

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }
        string strTenTB = "";
        private void txtten_Leave(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtten.Text))
            {
                if (txtten.Text.ToLower() != strTenTB.ToLower())
                {
                    ThongSoChungDTO _ts = new ThongSoChungDTO();
                    _ts.TENTB = txtten.Text;
                    MachSacBus _tsBUS = new MachSacBus();
                    int dt = _tsBUS.KT_TheoTenMachSac(_ts);
                    if (dt == 1)
                    {
                        txtten.Text = strTenTB;
                        MessageBox.Show("Thiết Bị Đã Tồn Tại, Bạn Có Thể Cập Nhật Lại Thiết Bị", "Thông Báo!");
                        txtten.Focus();
                    }
                }

            }
        }

        private void txtghichu_TextChanged(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void txtGia_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtCS_TextChanged(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void label10_Click(object sender, EventArgs e)
        {

        }
    }
}
